<template>
	<view class="container container329916">
		<u-navbar :isSlot="true" :isFixed="true" :isMarginRight="false" :borderBottom="false" title="" :background="{}" :backTextStyle="{ color: 'inherit' }" backIconColor="#fff" titleColor="inherit" :isHome="false" :isBack="false">
			<view class="flex align-center diygw-text-md text-bold align-center flex-nowrap diygw-col-24">
				<view class="flex flex-wrap diygw-col-0 items-center flex4-clz" @tap="navigateTo" data-type="backFunction">
					<text class="flex icon diygw-col-0 diy-icon-back"></text>
					<text class="diygw-col-0">
						{{ globalOption.from ? '首页' : '返回' }}
					</text>
				</view>
			</view>
		</u-navbar>
		<view class="flex flex-wrap diygw-col-24 flex-direction-column items-center flex7-clz">
			<!-- #ifdef MP-WEIXIN -->
			<view class="flex flex-wrap diygw-col-24 flex-direction-column flex8-clz"> </view>
			<!--  #endif -->
			<image src="/static/q.png" class="image3-size diygw-image diygw-col-0 image3-clz" mode="widthFix"></image>
			<text class="diygw-col-0 text1-clz"> DIY可视化商城系统 </text>
		</view>
		<!-- #ifdef H5||APP -->
		<u-form :model="form1" :rules="form1Rules" :errorType="['message', 'toast']" ref="form1Ref" class="flex diygw-form diygw-col-24">
			<u-form-item class="diygw-col-24 username-clz" labelPosition="top" prop="username">
				<text class="diy-icon-people margin-right-xs" style="color: #636363; font-size: 32rpx"></text>
				<u-input :focus="form1Data.usernameFocus" placeholder="请输入用户名" v-model="form1.username"></u-input>
			</u-form-item>
			<u-form-item class="diygw-col-24 password-clz" labelPosition="top" prop="password">
				<text class="diy-icon-lock margin-right-xs" style="color: #636363; font-size: 32rpx"></text>
				<u-input :focus="form1Data.passwordFocus" placeholder="请输入密码" v-model="form1.password" type="password" :password-icon="true"></u-input>
			</u-form-item>
			<view class="flex diygw-col-24 justify-between flex-nowrap flex1-clz">
				<view class="flex flex-wrap diygw-col-0" @tap="navigateTo" data-type="page" data-url="/pages/null" data-redirect="1">
					<text class="diygw-col-0"> 还没有账号？ </text>
					<text @tap="navigateTo" data-type="page" data-url="/pages/reg" class="diygw-col-0 text2-clz"> 注册 </text>
				</view>
			</view>
			<text @click="submitForm1" class="diygw-col-24 gradual-green text4-clz"> H5进行模似登录测试 </text>
			<view class="flex flex-wrap diygw-col-0 items-center flex3-clz">
				<view class="flex flex-wrap diygw-col-0 items-center" @tap="navigateTo" data-type="agreeFunction">
					<text v-if="globalData.agree == '1'" class="flex icon3 diygw-col-0 icon3-clz diy-icon-roundcheck"></text>
					<text v-if="globalData.agree != '1'" class="flex icon2 diygw-col-0 icon2-clz diy-icon-round"></text>
					<text class="diygw-col-0"> 登录即同意并接受 </text>
				</view>
				<text @tap="navigateTo" data-type="page" data-url="/pages/null" data-newstype="privacy" class="diygw-col-0 text11-clz"> 《隐私权政策》 </text>
				<text class="diygw-col-0"> 和 </text>
				<text @tap="navigateTo" data-type="page" data-url="/pages/null" data-newstype="user" class="diygw-col-0 text13-clz"> 《用户协议》 </text>
			</view>
		</u-form>
		<!--  #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<view class="flex flex-wrap diygw-col-24 flex-direction-column items-center">
			<text @tap="navigateTo" data-type="login" data-logintype="weixin" data-loginurl="/login/xcxlogin" data-callback="usersApi" class="diygw-col-19 gradual-green text-clz"> 微信登录 </text>
		</view>
		<!--  #endif -->
		<view @touchmove.stop.prevent="" v-if="modal" class="diygw-modal bottom-modal" :class="modal" style="z-index: 1000000">
			<view class="diygw-dialog diygw-dialog-modal">
				<view class="justify-end diygw-bar">
					<view class="content"> 完善用户信息 </view>
					<view class="action" data-type="closemodal" data-id="modal" @tap="navigateTo">
						<text class="diy-icon-close"></text>
					</view>
				</view>
				<view>
					<view class="flex diygw-dialog-content">
						<u-form :model="form" :rules="formRules" :errorType="['message', 'toast']" ref="formRef" class="flex diygw-form diygw-col-24">
							<view class="flex diygw-col-24">
								<!-- #ifdef MP-WEIXIN -->
								<button open-type="chooseAvatar" class="diygw-avatar diygw-avatar-button avatar-avatar radius" @chooseavatar="chooseFormAvatar">
									<image mode="aspectFit" class="diygw-avatar-img radius" :src="form.avatar"></image>
								</button>
								<!--  #endif -->
								<!-- #ifndef MP-WEIXIN -->
								<view class="diygw-avatar diygw-avatar-button radius" @tap="uploadFormAvatar">
									<image mode="aspectFit" class="diygw-avatar-img radius" :src="form.avatar"></image>
								</view>
								<!--  #endif -->
							</view>
							<text class="diygw-col-24 text5-clz"> 点击上方图片完善用户头像 </text>
							<u-form-item labelAlign="justify" class="diygw-col-24" :required="true" label="姓名" prop="nickname">
								<u-input :focus="formData.nicknameFocus" placeholder="请输入姓名" v-model="form.nickname" type="nickname"></u-input>
							</u-form-item>
							<u-form-item labelAlign="justify" class="diygw-col-24" :required="true" label="手机" prop="phone">
								<u-input :focus="formData.phoneFocus" placeholder="请输入手机号码" v-model="form.phone"></u-input>
							</u-form-item>
							<text @click="submitForm" class="diygw-col-24 gradual-green text6-clz"> 保存 </text>
						</u-form>
					</view>
				</view>
			</view>
		</view>
		<view class="clearfix"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//用户全局信息
				userInfo: {},
				//页面传参
				globalOption: {},
				//自定义全局变量
				globalData: { logintype: '0', agree: '0' },
				users: {
					rows: [],
					total: 0,
					code: 200,
					msg: 'success'
				},
				modal: '',
				form: {
					avatar: '/static/avatar.png',
					nickname: '',
					phone: ''
				},
				formRules: {
					nickname: [
						{
							trigger: ['change', 'blur'],
							required: true,
							message: '姓名不能为空'
						}
					],
					phone: [
						{
							trigger: ['change', 'blur'],
							required: true,
							message: '手机号不能为空'
						}
					]
				},
				form1Data: {
					usernameFocus: false,
					passwordFocus: false
				},
				formData: {
					nicknameFocus: false,
					phoneFocus: false
				},
				form1: {
					username: '',
					password: ''
				},
				form1Rules: {
					username: [
						{
							trigger: ['change', 'blur'],
							required: true,
							message: '用户名不能为空'
						}
					],
					password: [
						{
							trigger: ['change', 'blur'],
							required: true,
							message: '密码不能为空'
						}
					]
				}
			};
		},
		onShow() {
			this.setCurrentPage(this);
		},
		onLoad(option) {
			this.setCurrentPage(this);
			if (option) {
				this.setData({
					globalOption: this.getOption(option)
				});
			}

			this.init();
		},
		onReady() {
			this.$refs.form1Ref?.setRules(this.form1Rules);
			this.$refs.formRef?.setRules(this.formRules);
		},
		methods: {
			async init() {
				await this.initResetform1();
				await this.initResetform();
			},
			// 获取用户 API请求方法
			async usersApi(param) {
				let thiz = this;
				param = param || {};

				//请求地址及请求数据，可以在加载前执行上面增加自己的代码逻辑
				let http_url = '/diy/user/list';
				let http_data = {
					openid: param.openid || this.userInfo.openid || '1'
				};
				let http_header = {};

				let users = await this.$http.post(http_url, http_data, http_header, 'json');

				this.globalData.isShow = true;

				this.users = users;
				if (users.rows.length == 1) {
					let user = users.rows[0];
					if (!user.nickname) {
						this.navigateTo({
							type: 'openmodal',
							id: 'modal'
						});
					} else {
						uni.navigateBack();
					}
				}
			},

			// 同意或不同意 自定义方法
			async agreeFunction(param) {
				let thiz = this;
				//如果不同意，改为同意
				this.globalData.agree = this.globalData.agree == '1' ? '0' : '1';
			},

			// 判断是否登录 自定义方法
			async isloginFunction(param) {
				let thiz = this;
				if (this.$session.getToken()) {
					//比如未登录，转身到其他页面等
					this.showToast('请先登录');
					this.navigateTo({
						redirect: 1,
						type: 'page',
						url: 'index'
					});
					return;
				}
			},

			// 回退 自定义方法
			async backFunction(param) {
				let thiz = this;
				if (this.globalOption.form) {
					this.navigateTo({
						type: 'page',
						url: 'index',
						redirect: '1'
					});
				} else {
					uni.navigateBack();
				}
			},
			initResetform1() {
				this.initform1 = JSON.stringify(this.form1);
				//如果想给表单默认初始值，其中row为某一行数据也可能是API返回的结果集，然后给到this.form1
				//this.form1 = this.$tools.changeRowToForm(row,this.form1)
			},
			resetForm1() {
				this.form1 = JSON.parse(this.initform1);
			},

			async submitForm1(e) {
				this.$refs.form1Ref?.setRules(this.form1Rules);

				this.$nextTick(async () => {
					let valid = await this.$refs.form1Ref.validate();
					if (valid) {
						//保存数据
						let param = this.form1;
						let header = {};
						let url = '/login/login';

						if (this.globalData.agree != '1') {
							this.showToast('请点击同意授权协议');
							return;
						}
						param.logintype = this.globalData.logintype;
						let res = await this.$http.post(url, param, header, 'json');
						if (res.code == 200) {
							this.$session.setUser(res.data);
							this.showToast(res.msg, 'success');
							this.navigateTo({
								type: 'page',
								url: 'index'
							});
						} else {
							this.showToast(res.msg, 'error');
						}
						if (res.code == 200) {
							this.showToast(res.msg, 'success');
						} else {
							this.showModal(res.msg, '提示', false);
						}
					} else {
						console.log('验证失败');
					}
				});
			},
			chooseFormAvatar(e) {
				const { avatarUrl } = e.detail;
				let baseavatarUrl = 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(avatarUrl, 'base64');
				this.form.avatar = baseavatarUrl;
			},
			async uploadFormAvatar(e) {
				await this.uploadImage(this, 'form.avatar', 'formData.avatarDatas', '/sys/storage/upload', 1, 'avatar');
			},
			initResetform() {
				this.initform = JSON.stringify(this.form);
				//如果想给表单默认初始值，其中row为某一行数据也可能是API返回的结果集，然后给到this.form
				//this.form = this.$tools.changeRowToForm(row,this.form)
			},
			resetForm() {
				this.form = JSON.parse(this.initform);
			},

			async submitForm(e) {
				this.$refs.formRef?.setRules(this.formRules);

				this.$nextTick(async () => {
					let valid = await this.$refs.formRef.validate();
					if (valid) {
						//保存数据
						let param = this.form;
						let header = {};
						let url = '/diy/user/update';

						param.id = this.userInfo.id;
						if (!this.form.avatar) {
							this.showToast('请上传头像');
							return;
						}
						let res = await this.$http.post(url, param, header, 'json');
						if (res.code == 200) {
							this.showToast('保存成功');

							let user = this.$session.getUser();
							user.nickname = this.form.nickname;
							user.phone = this.form.phone;
							user.avatar = res.data.avatar;
							this.$session.setUser(user);
							uni.navigateBack();
						} else {
							this.showToast('保存失败，请重试');
						}
					} else {
						console.log('验证失败');
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.flex4-clz {
		padding-top: 6rpx;
		padding-left: 10rpx;
		padding-bottom: 6rpx;
		padding-right: 6rpx;
	}
	.icon {
		font-size: 36rpx;
	}
	.flex7-clz {
		padding-top: 80rpx;
		padding-left: 10rpx;
		padding-bottom: 40rpx;
		padding-right: 10rpx;
	}
	.flex8-clz {
		height: 120rpx;
	}
	.image3-clz {
		flex-shrink: 0;
		border-bottom-left-radius: 120rpx;
		overflow: hidden;
		width: 120rpx !important;
		border-top-left-radius: 120rpx;
		border-top-right-radius: 120rpx;
		border-bottom-right-radius: 120rpx;
		height: 120rpx !important;
	}
	.image3-size {
		height: 120rpx !important;
		width: 120rpx !important;
	}
	.text1-clz {
		margin-left: 10rpx;
		padding-top: 10rpx;
		font-weight: bold;
		padding-left: 20rpx;
		font-size: 32rpx !important;
		padding-bottom: 10rpx;
		margin-top: 0rpx;
		margin-bottom: 10rpx;
		margin-right: 10rpx;
		padding-right: 20rpx;
	}
	.username-clz {
		margin-left: 10rpx;
		width: calc(100% - 10rpx - 10rpx) !important;
		font-size: 28rpx !important;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		margin-right: 10rpx;
	}
	.password-clz {
		margin-left: 10rpx;
		width: calc(100% - 10rpx - 10rpx) !important;
		font-size: 28rpx !important;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		margin-right: 10rpx;
	}
	.flex1-clz {
		padding-top: 16rpx;
		padding-left: 30rpx;
		padding-bottom: 16rpx;
		padding-right: 30rpx;
	}
	.text2-clz {
		color: #39b54a;
	}
	.text4-clz {
		padding-top: 20rpx;
		border-bottom-left-radius: 120rpx;
		color: #ffffff;
		font-weight: bold;
		letter-spacing: 6rpx !important;
		padding-left: 10rpx;
		font-size: 32rpx !important;
		padding-bottom: 20rpx;
		border-top-right-radius: 120rpx;
		margin-right: 30rpx;
		margin-left: 30rpx;
		box-shadow: 0rpx 6rpx 14rpx rgba(64, 195, 3, 0.35);
		overflow: hidden;
		width: calc(100% - 30rpx - 30rpx) !important;
		border-top-left-radius: 120rpx;
		margin-top: 10rpx;
		border-bottom-right-radius: 120rpx;
		margin-bottom: 10rpx;
		text-align: center;
		padding-right: 10rpx;
	}
	.flex3-clz {
		padding-top: 16rpx;
		padding-left: 30rpx;
		padding-bottom: 16rpx;
		padding-right: 30rpx;
	}
	.icon3-clz {
		color: #39b54a;
	}
	.icon3 {
		font-size: 40rpx;
	}
	.icon2-clz {
		color: #39b54a;
	}
	.icon2 {
		font-size: 40rpx;
	}
	.text11-clz {
		color: #39b54a;
	}
	.text13-clz {
		color: #39b54a;
	}
	.text-clz {
		padding-top: 20rpx;
		border-bottom-left-radius: 120rpx;
		color: #ffffff;
		font-weight: bold;
		letter-spacing: 6rpx !important;
		padding-left: 100rpx;
		font-size: 32rpx !important;
		padding-bottom: 20rpx;
		border-top-right-radius: 120rpx;
		margin-right: 30rpx;
		margin-left: 30rpx;
		box-shadow: 0rpx 6rpx 14rpx rgba(64, 195, 3, 0.35);
		overflow: hidden;
		width: calc(79.1666666667% - 30rpx - 30rpx) !important;
		border-top-left-radius: 120rpx;
		margin-top: 10rpx;
		border-bottom-right-radius: 120rpx;
		margin-bottom: 10rpx;
		text-align: center;
		padding-right: 100rpx;
	}
	.modal-clz {
		z-index: 1000000;
	}
	.diygw-dialog-modal {
	}
	.text5-clz {
		margin-left: 10rpx;
		color: #c2c2c2;
		width: calc(100% - 10rpx - 10rpx) !important;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		text-align: center;
		margin-right: 10rpx;
	}
	.text6-clz {
		padding-top: 20rpx;
		border-bottom-left-radius: 120rpx;
		color: #ffffff;
		font-weight: bold;
		letter-spacing: 6rpx !important;
		padding-left: 10rpx;
		font-size: 32rpx !important;
		padding-bottom: 20rpx;
		border-top-right-radius: 120rpx;
		margin-right: 30rpx;
		margin-left: 30rpx;
		box-shadow: 0rpx 6rpx 14rpx rgba(64, 195, 3, 0.35);
		overflow: hidden;
		width: calc(100% - 30rpx - 30rpx) !important;
		border-top-left-radius: 120rpx;
		margin-top: 10rpx;
		border-bottom-right-radius: 120rpx;
		margin-bottom: 40rpx;
		text-align: center;
		padding-right: 10rpx;
	}
	.container329916 {
		background-image: url(/static/mbz704.png);
		background-position: top center;
		background-size: contain;
		background-repeat: no-repeat;
	}
</style>
